package com.wzxy.pf.bill.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzxy.pf.bill.dto.BillDTO;
import com.wzxy.pf.bill.entity.Bill;
import com.wzxy.pf.bill.service.BillService;
import com.wzxy.pf.common.dto.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    BillService billService;
    @PostMapping("/saveBill")
    public ResponseDTO saveBill(@RequestBody Bill bill){
        bill.setCreateTime(LocalDateTime.now());
        bill.setUpdateTime(LocalDateTime.now());
        bill.setBillTime(String.valueOf(LocalDateTime.now()));
        boolean result = this.billService.saveOrUpdate(bill);
//        if(result){
//            return "success";
//        }else {
//            return "fail";
//        }
            ResponseDTO responseDTO = new ResponseDTO();
            if (true == result){
                responseDTO.setCode(200);
                responseDTO.setMessage("保存成功");
            }else {
                responseDTO.setCode(500);
                responseDTO.setMessage("保存失败");
            }
            return responseDTO;
    }
    @PostMapping("/updateBill")
    public ResponseDTO updateBill(@RequestBody Bill bill){
        boolean result = this.billService.saveOrUpdate(bill);
        bill.setUpdateTime(LocalDateTime.now());
        ResponseDTO responseDTO = new ResponseDTO();
        if (true == result){
            responseDTO.setCode(200);
            responseDTO.setMessage("更新成功");
        }else {
            responseDTO.setCode(500);
            responseDTO.setMessage("更新失败");
        }
        return responseDTO;
    }
    @PostMapping("/deletebillsByIds")
    public ResponseDTO deletebillsByIds(@RequestBody List<Long> ids){
        boolean result = this.billService.removeByIds(ids);
        ResponseDTO responseDTO = new ResponseDTO();
        if (true == result){
            responseDTO.setCode(200);
            responseDTO.setMessage("删除成功");
        }else {
            responseDTO.setCode(500);
            responseDTO.setMessage("删除失败");
        }
        return responseDTO;
    }
    @PostMapping("/selectUsersByPage")
    public Page<Bill> selectUsersByPage(@RequestBody BillDTO billDTO){
        return this.billService.selectBillsByPage(billDTO);
    }
}
